import Vuex from "vuex";

export default {
    namespaced: true,
    state: {
        list: {},
        all:[],
        map: new Map(),
        modelTree: [],
    },
    getters: {
        getBy: (state) => (type, className) => {
            return state.map.get([type, className]);
        },
    },
    mutations: {
        setAll(state, all) {
            state.list = all;
            let modelTree = [];
            let map = new Map();
            Object.keys(state.list).map((tableName) => {
                state.list[tableName].map((item) => {
                    //model树形结构
                    let index = modelTree.findIndex((m) => m.value === item.type);
                    if (index === -1) {
                        let obj = {
                            label: item.type,
                            value: item.type,
                            children: [
                                {
                                    label: item.className,
                                    value: item.className,
                                },
                            ],
                        };
                        modelTree.push(obj);
                    } else {
                        modelTree[index].children.push({
                            label: item.className,
                            value: item.className,
                        });
                    }
                    let val = {
                        table: tableName,
                        featureList: new Set(),
                        valueList: new Set(),
                        fieldList: new Set(),
                        groupList: new Set(),
                        fieldMap: {},
                        featureMap: {},
                    };
                    item.features.map((f) => {
                        val.featureList.add(f.fieldName);
                        val.fieldList.add(f.fieldName);
                        val.fieldMap[f.fieldName] = f.fieldType;
                        val.featureMap[f.fieldName] = f.fieldType;
                    });
                    Object.keys(item.values).map((g) => {
                        val.groupList.add(g);
                        item.values[g].map((p) => {
                            val.valueList.add(p.propertyName);
                            val.fieldList.add(p.propertyName);
                            val.fieldMap[p.propertyName] = p.propertyType;
                        });
                    });
                    //   map.set([item.type, item.className], val);
                    map.set([item.type, item.className].join(','), val);
                });
            });
            state.map = map;
            state.modelTree = modelTree;
            // console.log(map)
        },
    },
    actions:{
        setAll({state,commit}, all) {
            let list={};
            Object.keys(all).map(key=>{
                let item=all[key].filter(item=>item.published===true);
                list[key]=item;
                state.all=state.all.concat(item);
            })
            state.list =list;
            // commit('formatAll');
            // console.log(state)
        },
        getModel({state, commit}, val) {
            let res = state.all.filter(item => item.type === val.type && item.className === val.className);
            return res.length ? res[0] : null
        },
        getModelAll({state, commit}) {
            return state.all
        }
    }
}